<!--
 * @Author: your name
 * @Date: 2022-04-19 12:12:33
 * @LastEditTime: 2022-05-22 19:25:55
 * @LastEditors: qianduanxuyaochendian lxy_01@outlook.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \job-master\web\src\views\job\Job-detail.vue
-->
<template>
  <div class="container">
    <div class="banner-container">
      <div class="logo">
        <n-image width="100" :src="avatar" />
      </div>
      <div class="item-left">
        <div class="status">{{ jobStatus }}</div>
        <div class="job-content">
          <div class="title">
            <div class="name">
              <n-gradient-text :size="36" type="success">
                {{ jobInfo.name }}
              </n-gradient-text>
            </div>
          </div>
          <div class="demand">
            {{ jobInfo.city }}
            <n-divider vertical />
            {{ jobInfo.academic }}
            <n-divider vertical />
            {{ jobInfo.experienceRequire }}
          </div>
          <div class="salary">
            <n-icon>
              <svg-icon name="money" color="gold"></svg-icon>
            </n-icon>
            <b>{{ jobSalary }}</b>
          </div>
        </div>
      </div>
      <div class="item-right">
        <div class="Welfare">
          <n-space>
            全勤奖
            <n-divider vertical />带薪年假 <n-divider vertical />员工旅游
          </n-space>
        </div>
        <div class="upload">
          <n-button type="info" @click="sendResume">投递简历</n-button>
        </div>
      </div>
    </div>
    <div class="introduce-box">
      <div class="introduce">
        <div class="introduce-tag">
          <n-tag type="success" size="large" round>
            <h3>职位介绍</h3>
          </n-tag>
        </div>
        <div class="fold-text">
          <p class="require-list" v-for="(item, index) in jobDuty" :key="index">
            {{ index + 1 }}、{{ item }}
          </p>
        </div>
      </div>
      <div class="introduce" style="margin-top: 30px">
        <div class="introduce-tag">
          <n-tag type="success" size="large" round>
            <h3>职位要求</h3>
          </n-tag>
        </div>
        <div class="fold-text">
          <p
            class="require-list"
            v-for="(item, index) in jobRequirements"
            :key="index"
          >
            {{ index + 1 }}、{{ item }}
          </p>
        </div>
      </div>
      <div class="address" style="margin-top: 30px">
        <div class="map_container">
          <div class="location-address">
            <div class="tag">
              <n-tag type="info" size="large" round> 公司地址 </n-tag>
            </div>
            <div class="address-info">
              <n-space>
                <n-icon :size="18">
                  <svg-icon name="locate"></svg-icon>
                </n-icon>
                <n-gradient-text type="info">
                  {{ address }}
                </n-gradient-text>
              </n-space>
            </div>
          </div>
          <div class="img" id="mapcontainer"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import AMapLoader from "@amap/amap-jsapi-loader"
//引入查询工作详情的接口
import { getDetailAPI } from "../../http/api/company.js"
//引入投递简历信息的接口
import { sendResumeAPI } from "../../http/api/student.js"
export default {
  data() {
    return {
      map: null, //初始化 map 对象
      address: "",
      coordinate: [],
      jobInfo: {},
      tag: [],
    }
  },
  methods: {
    // 初始化地图方法
    async initMap() {
      AMapLoader.load({
        key: "3da863722b02771fec65faddf6e8c24f", //此处填入我们注册账号后获取的Key
        version: "2.0", //指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
        plugins: ["AMap.Geocoder"], //需要使用的的插件列表，如比例尺'AMap.Scale'等
      })
        .then((AMap) => {
          const self = this
          // 处理正向地理编码的方法
          new Promise((resolve, reject) => {
            AMap.plugin("AMap.Geocoder", function () {
              var geocoder = new AMap.Geocoder({
                // city 指定进行编码查询的城市，支持传入城市名、adcode 和 citycode
                city: self.address,
              })

              geocoder.getLocation(self.address, function (status, result) {
                if (status === "complete" && result.info === "OK") {
                  // result中对应详细地理坐标信息
                  self.coordinate.push(result.geocodes[0].location.lng)
                  self.coordinate.push(result.geocodes[0].location.lat)
                  // console.log("getLocation", self.coordinate)
                  resolve({ AMap, LngAndLat: self.coordinate, self }) //resolve只能传递一个参数，同时传递多个参数的情况下使用对象传参
                } else {
                  console.log("ERR")
                }
              })
            })
          }).then((res) => {
            // console.log(res)
            res.self.map = new res.AMap.Map("mapcontainer", {
              //设置地图容器id
              viewMode: "3D", //是否为3D地图模式
              zoom: 16, //初始化地图级别
              center: res.LngAndLat, //初始化地图中心点位置
            })
            // 添加地图缩放工具插件
            // res.AMap.plugin("AMap.ToolBar", function () {
            //   //异步加载插件
            //   var toolbar = new res.AMap.ToolBar()
            //   res.self.map.addControl(toolbar)
            // })
            // 缩略图插件
            res.AMap.plugin("AMap.HawkEye", function () {
              //异步加载插件
              var hawkeye = new res.AMap.HawkEye()
              res.self.map.addControl(hawkeye)
            })
            //实例化标点对象
            var marker = new res.AMap.Marker({
              position: new AMap.LngLat(res.LngAndLat[0], res.LngAndLat[1]), // 经纬度对象，也可以是经纬度构成的一维数组[116.39, 39.9]
              title: res.self.address,
            })
            // 给地图实例对象添加标点
            res.self.map.add(marker)
          })
        })

        .catch((e) => {
          console.log(e)
        })
    },
    //获取工作详情
    getDetailInfo() {
      const param = {
        id: this.$route.query.id,
      }
      getDetailAPI(param)
        .then((result) => {
          this.jobInfo = result.result[0]
          this.address = result.result[0].address
          // this.$nextTick 将回调延迟到下次DOM更新循环之后执行。在修改数据之后立即使用它，然后等待DOM更新。
          this.$nextTick(() => {
            this.tag = JSON.parse(result.result[0].tag).join(",").split(",")
            this.initMap()
          })
        })
        .catch((err) => {
          console.log(err)
        })
    },
    //实现投递简历到当前页面的公司
    sendResume() {
      const data = {
        company_id: this.jobInfo.c_id,
        apply_job: this.jobInfo.name,
      }
      console.log(data)
      sendResumeAPI(data)
        .then((result) => {
          if (result.status === true) {
            window.$message.success("投递成功")
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
  },
  computed: {
    jobStatus() {
      return this.jobInfo.status == 1 ? "招聘中" : "停止招聘"
    },
    avatar() {
      return process.env.VUE_APP_SERVER + this.jobInfo.logo
    },
    jobSalary() {
      return this.jobInfo.salary
        ? JSON.parse(this.jobInfo.salary).join("-")
        : ""
    },
    jobDuty() {
      return this.jobInfo.duty ? JSON.parse(this.jobInfo.duty) : ""
    },
    jobRequirements() {
      return this.jobInfo.requirements
        ? JSON.parse(this.jobInfo.requirements)
        : ""
    },
  },
  mounted() {
    //初始化查询工作详情
    this.getDetailInfo()
  },
}
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;

  .banner-container {
    display: flex;
    justify-content: flex-start;
    flex-direction: row;
    align-items: center;
    width: 100%;
    height: 25%;
    background-color: #444c5f;

    .logo {
      margin-left: 50px;
    }

    .item-left {
      margin-left: 100px;
      width: 400px;
      height: 120px;

      .status {
        color: white;
      }

      .job-content {
        .title {
          display: flex;
          flex-direction: row;
          justify-content: flex-start;
          gap: 20px;
          margin-top: 15px;
          margin-bottom: 15px;

          .name {
          }
        }

        .demand {
          color: white;
        }

        .salary {
          padding: 10px 0px;
          display: flex;
          align-items: center;
          color: gold;

          b {
            padding-left: 10px;
          }
        }
      }
    }

    .item-right {
      margin-left: 25%;
      width: 400px;
      height: 150px;
      display: flex;
      flex-direction: column;
      justify-content: space-around;

      .Welfare {
        color: white;
      }

      .upload {
      }
    }
  }

  .introduce-box {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;

    .introduce {
      margin: 20px 0px;
      border-radius: 5px;
      box-shadow: rgba(0, 0, 0, 0.16) 0px 1px 4px;
      background-color: #fff;
      width: 90%;

      .require-list {
        padding: 15px;
      }

      .introduce-tag {
        margin-top: 1%;
        margin-left: 1%;
      }

      .n-tag__content {
        margin-bottom: 20px;
      }

      .fold-text {
        margin-left: 1%;
        color: #51586d;
      }

      h3 {
        font-weight: 700;
        font-size: 15px;
      }
    }

    .address {
      width: 90%;
      height: 450px;
      margin-bottom: 20px;
      box-shadow: rgba(0, 0, 0, 0.16) 0px 1px 4px;
      background-color: #fff;

      .map_container {
        .location-address {
          margin-top: 1%;
          margin-left: 1%;
          display: flex;
          justify-content: flex-start;
          align-items: center;
          gap: 15px;
        }

        .img {
          width: 98%;
          height: 390px;
          margin: 1%;
        }
      }
    }
  }
}
</style>
